package com.neuedu.lambda;


import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 *  Lambda
 */
public class MyLambda {

    public static void main(String[] args) {


        MyMathOperation myMathOperation1 =  (int numa, int numb) -> {   return numa + numb;   };
        System.out.println(myMathOperation1.cal(10,20));

        MyMathOperation myMathOperation2 =  (int numa, int numb) -> {   return numa - numb;   };
        System.out.println(myMathOperation2.cal(10,20));



//        Function<String,Integer> func = new Function<String, Integer>() {
//            @Override
//            public Integer apply(String s) {
//                return Integer.valueOf(s) *2;
//            }
//        };

//        Function<String,Integer> f =(String s)-> {  return Integer.valueOf(s) *2;  };

        //一个参数的 可以省略参数列表的括号
//        Function<String,Integer> f =  s-> {  return Integer.valueOf(s) *2;  };

        //如果lambda表达式只有一句话，那么该表达式的值 作为 lambda的返回结果
        Function<String,Integer> f =  s ->   Integer.valueOf(s) *2 ;

        Integer  result  = f.apply("60");
        System.out.println("result = " + result);


        //一个参数都没有，不允许省略括号
        Supplier<Long> su = () ->  System.currentTimeMillis();
        System.out.println(su.get());
        System.out.println(su.get());
        System.out.println(su.get());
        System.out.println(su.get());
        System.out.println(su.get());
        System.out.println(su.get());


        List list = new ArrayList();
        list.add(10);
        list.add(15);
        list.add(20);
        list.add(25);
        list.add(30);
        list.add(35);


        //过滤能被10 整除
        List newList = filter(list, (num) -> {
            return num % 10 == 0;
        });

        for (Object o : newList) {
            System.out.println("o = " + o);
        }


        //不能被 10整除
        newList = filter(list, num -> num % 10 != 0);

        for (Object o : newList) {
            System.out.println("o = " + o);
        }

    }


    /**
     * 过滤的方法
     * @param list
     * @param predicate
     * @return
     */
    public static List<Integer> filter(List<Integer>  list , Predicate<Integer> predicate){
        List newList = new ArrayList();//过滤之后的结果

        for (Integer num : list) {
            if(predicate.test(num)){
                newList.add(num);
            }
        }

        return  newList;
    }




}
